//
//  DDHotmusicController.m
//  DDMusic
//
//  Created by rimi on 16/11/10.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDHotmusicController.h"
#import "DDHotmusicCell.h"
#import "DDHotmusicModel.h"
#import "DDNetWorkTools.h"
#import "DDPublicMacro.h"
#import "DDMusicModel.h"

@interface DDHotmusicController ()
@property(nonatomic,strong)NSMutableArray* dataArray;
@end

@implementation DDHotmusicController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.estimatedRowHeight = 106;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    NSString* url = @"http://route.showapi.com/213-4";
    __weak typeof(self)myself = self;
    NSString* showapi_appid = @"27019";
    NSString* showapi_sign = @"53931f9c4a2f4ed0ab945cb3757b4f94";
    NSString* topid = @"26";
    NSDictionary* parma = @{@"showapi_appid":showapi_appid,@"showapi_sign":showapi_sign,@"topid":topid};
    [[DDNetWorkTools shareDDNetWorkTools]requestWithMethod:GET Url:url Parameters:parma finished:^(id responseObject, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            NSLog(@"出现异常");
            return ;
        }
        //        NSLog(@"从这里开始");
        //        NSLog(@"%@",responseObject);
        NSArray* dictArray = responseObject[@"showapi_res_body"][@"pagebean"][@"songlist"];
        _dataArray = [NSMutableArray arrayWithCapacity:dictArray.count];
        for (NSDictionary* dict in dictArray) {
            DDHotmusicModel* hot = [[DDHotmusicModel alloc]initWithDict:dict];
            [_dataArray addObject:hot];
        }
        [myself.tableView reloadData];
    }];

    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DDHotmusicCell *cell = [[NSBundle mainBundle]loadNibNamed:@"DDHotmusicCell" owner:nil options:nil].firstObject;
    DDHotmusicModel* hotmodel = self.dataArray[indexPath.row];
    cell.musics = hotmodel;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DDHotmusicCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DDMainRankTableViewCellClicked" object:@{ISPLAYNEWMUSIC:@"YES",NEWPLAYMUSICLIST:@[(DDMusicModel*)cell.musics]}];
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
